package com.bookshop.service.impl;

import com.bookshop.entity.Book;
import com.bookshop.entity.BookList;
import com.bookshop.entity.Collect;
import com.bookshop.mapper.CollectMapper;
import com.bookshop.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: CollectServiceImp
 * Package: com.bookshop.service.impl
 * Description:
 *
 * @Author BJX
 * @Create 2024/1/21 11:33
 * @Version 1.0
 */
@Service
public class CollectServiceImp implements CollectService {
    private CollectMapper collectMapper;
    @Autowired
    public CollectServiceImp(CollectMapper collectMapper) {
        this.collectMapper = collectMapper;
    }

    @Override
    public int addCollect(long bookId,long userId) {
        //判断用户是否已经收藏该图书
        int res = collectMapper.searchBookInCollect(bookId,userId);
        //已收藏
        if(res == 1) {
            return -1;
        }
        return collectMapper.addCollect(bookId,userId);
    }

    @Override
    public int cancelCollect(long bookId, long userId) {
        //判断用户是否已经收藏该图书
        int res = collectMapper.searchBookInCollect(bookId,userId);
        //未收藏
        if(res == 0) {
            return -1;
        }
        return collectMapper.cancelCollect(bookId,userId);

    }

    @Override
    public int countCollect(long userId) {
        return collectMapper.countCollect(userId);
    }

    @Override
    public int clearCollect(long userId){
        return collectMapper.clearCollect(userId);
    }

    @Override
    public List<Collect> showCollect(int pageNum, long userId) {
        PageHelper.startPage(pageNum, 8);
        List<Collect> collects=collectMapper.showCollect(userId);
        PageInfo<Collect> pageInfo = new PageInfo<>(collects);
        return pageInfo.getList();
    }

}
